﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Entity;
using BAL;
using Common;
using System.IO;

namespace Web
{
    public partial class PostFiles : System.Web.UI.Page
    {
        public int editid = 0, delid = 0,page=1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "MasterFile"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }

            if (Request.QueryString["editid"] != null)
            {
                editid = Convert.ToInt32(Request.QueryString["editid"].Trim());
            }
            if (Request.QueryString["delid"] != null)
            {
                delid = Convert.ToInt32(Request.QueryString["delid"].Trim());
            }
            if (Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(Request.QueryString["page"].Trim());
            }
            if (!IsPostBack)
            {
               
                LoadList();
                if (editid > 0)
                {
                    FilesInfo finfo = new FilesInfo();
                    finfo = Files.GetT1(editid);
                    Name.Text = finfo.Name;
                    SubjectList.SelectedValue = finfo.Subject.ToString();
                }
                if (delid > 0)
                {
                    FilesInfo finfo = new FilesInfo();
                    finfo = Files.GetT1(delid);
                    if (File.Exists(Server.MapPath(finfo.Patch + finfo.FileName)))
                    {
                        File.Delete(Server.MapPath(finfo.Patch + finfo.FileName));
                    }
                    if (File.Exists(Server.MapPath(finfo.Patch + finfo.Minipic)))
                    {
                        File.Delete(Server.MapPath(finfo.Patch + finfo.Minipic));
                    }
                    Files.Delete(delid);
                    Response.Redirect("AdminFiles.aspx?page=" + page); 
                }
                
            }
        }

        protected void LoadList()
        {
            SubjectList.Items.Clear();
            SubjectList.Items.Add(new ListItem("不分类", "0"));
            IList<SubjectInfo> subjectInfos = Subject.GetSubjectInfos();
            foreach (SubjectInfo info in subjectInfos)
            {
                SubjectList.Items.Add(new ListItem(info.Name, info.Id.ToString()));
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Name.Text.Trim() == "")
            {
                Utility.ShowJsAlert("名称不能为空", this);
                return;
            }
            //Button1.Enabled = false;
            FilesInfo finfo = new FilesInfo();

            if (editid > 0)
            {
                finfo = Files.GetT1(editid);
                finfo.Name = Name.Text;
                finfo.Subject = int.Parse(SubjectList.SelectedValue.Trim());
                Files.Edit(finfo);
                Utility.ReloadTopPage(this);
            }
            else
            {
                try
                {
                    if (FileUpload1.HasFile)
                    {
                        
                        finfo.Name = Name.Text;
                        finfo.Subject = int.Parse(SubjectList.SelectedValue.Trim());
                        Module_File mf = new Module_File();
                        finfo.FileExt = Path.GetExtension(FileUpload1.FileName);
                        if (!mf.CheckExt(finfo.FileExt))
                        {
                            Utility.ShowJsAlert("您上传的文件类型不在允许范围内", this);
                            return;
                        }
                        finfo.FileSize = FileUpload1.PostedFile.ContentLength.ToString();
                        if (!mf.CheckFileSize(long.Parse(finfo.FileSize)))
                        {
                            Utility.ShowJsAlert("您上传的文件过大，最大上传：" + (mf.MaxSize / 1024).ToString() + "kb", this);
                            return;
                        }
                        Button1.Enabled = false;
                        finfo.FileName = mf.GetFileName(finfo.FileExt);
                        finfo.Patch = mf.GetDirName();
                        if (Session["userid"] == null)
                        {
                            finfo.UserId = 0;//在这里需要添加用户的Id
                        }
                        else 
                        {
                            finfo.UserId = Convert.ToInt32(Session["userid"]);//在这里需要添加用户的Id    
                        } 
                        
                        mf.CreateUpfileDirectory(Server.MapPath(finfo.Patch));
                        FileUpload1.PostedFile.SaveAs(Server.MapPath(finfo.Patch + finfo.FileName));
                        finfo.Minipic = finfo.FileName.Replace(finfo.FileExt, "_s" + finfo.FileExt);
                        ImageLibrary.Basic im = new ImageLibrary.Basic();
                        im.reimgsize(Server.MapPath(finfo.Patch + finfo.FileName), Server.MapPath(finfo.Patch+finfo.Minipic), 120, 90);
                        Files.Add(finfo);
                        Utility.ReloadTopPage(this);
                    }
                    else
                    {
                        Utility.ShowJsAlert("必须选择上传文件", this);
                        return;
                    }
                }
                catch
                {
                    Utility.ShowJsAlert("上传失败", this);
                    return;
                }
            }

        }
    }
}
